<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/20
 * Time: 下午5:26
 */

namespace App\Console\Commands;

use App\Modules\Agent\Services\AgentServices;
use App\Modules\Channel\Services\ChannelDailyStatService;
use App\Modules\SendOrder\Services\SendOrderService;
use App\Modules\Trade\Services\OrderService;
use DB;
use Illuminate\Console\Command;
use Log;

class GenChannelDayStat extends Command
{
    /**
     * 执行命令 获取站点的派单数据及服务号信息
     *
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'gen_channel_day_stat';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '获取站点的派单数据及服务号信息';

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $out_channels = explode(',', env('OUT_CHANNEL_ID_LIST'));
        $inner_channels = explode(',', env('INNER_CHANNEL_ID_LIST'));
        $distribution_channel_ids = array_merge($out_channels, $inner_channels);
        /*$distribution_channel_ids = [123, 8, 211, 5, 146, 160, 155, 4236, 255, 4237, 256, 4241, 691, 273,
            722, 148, 4364, 266, 695, 202, 4174, 271, 4025, 4593, 4426, 4053, 4334];*/

        $date = date('Y-m-d', strtotime('-1 day'));
        $result = SendOrderService::getSendOrdersStats($distribution_channel_ids, $date);

        if ($result) {
            foreach ($result as $item) {
                $appid = $item->appid;
                $new_subscribe_num = 0;
                $total_subscribe_num = 0;
                $cancel_subscribe_num = 0;
                $cancel_subscribe_rate = '0%';
                $distribution_channel_id = $item->id;
                $company_name = $item->principal_name;
                $official_account_name = $item->nickname;
                $cost = $item->totalCost ? $item->totalCost : 0;
                $agentInfo = AgentServices::getAgentByChannelId($distribution_channel_id);
                $agent_name = $agentInfo ? $agentInfo->agent_name : '';
                $promotion_point = $agentInfo ? $agentInfo->agent_intro : '';
                $charge_amount = OrderService::getChannelDateOrderInfo($distribution_channel_id, $date);
                $data = compact('distribution_channel_id', 'cancel_subscribe_rate', 'cost', 'official_account_name', 'total_subscribe_num', 'new_subscribe_num', 'cancel_subscribe_num', 'company_name', 'charge_amount', 'date', 'agent_name', 'promotion_point', 'appid');
                ChannelDailyStatService::addItem($data);
            }
        }
    }
}